package dream.web;

import com.alibaba.fastjson.JSON;
import dream.pojo.Article;
import dream.pojo.User;
import dream.service.ArticleService;
import dream.util.SessionUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 获取我的文章详情
 */
@WebServlet("/myarticlelist")
public class MyArticleListServlet extends HttpServlet {
    private ArticleService article = new ArticleService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user= SessionUtils.getLoginUser(req);
        if(user!=null){
            int id = user.getId();
            List<Article> list = article.SelectById(id);

            // 转为JSON
            String jsonString = JSON.toJSONString(list);

            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write(jsonString);

        }else{

        }
    }
}
